package cn.yuanqiao.archive.management.service.impl;

import cn.yuanqiao.archive.management.domain.YqXzjl;
import cn.yuanqiao.archive.management.domain.YqZt;
import cn.yuanqiao.archive.management.mapper.YqZpjlMapper;
import cn.yuanqiao.archive.management.mapper.YqZtMapper;
import cn.yuanqiao.archive.management.service.IYqZtService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 载体Service业务层处理
 * 
 * @author sy
 * @date 2023-07-10
 */
@Service
public class YqZtServiceImpl implements IYqZtService 
{
    @Autowired
    private YqZtMapper yqZtMapper;

    @Autowired
    private YqZpjlMapper yqZpjlMapper;

    /**
     * 查询载体
     * 
     * @param ztId 载体主键
     * @return 载体
     */
    @Override
    public YqZt selectYqZtByZtId(Long ztId)
    {
        YqZt yqZt = new YqZt();
        yqZt.setZtId(ztId);
        return yqZtMapper.selectYqZtByZtId(yqZt);
    }

    /**
     * 查看载体是否挂载，剩余容量
     *
     * @param ztId 载体主键
     * @return 载体
     */
    @Override
    public YqZt confirm(Long ztId)
    {
        YqZt yqZt = new YqZt();
        yqZt.setZtId(ztId);
        if (null!=yqZtMapper.selectYqZtByZtId(yqZt)){
            String place = "";
            yqZt = yqZtMapper.selectYqZtByZtId(yqZt);
            place = yqZt.getSavePath();
            String driveLetter = place.charAt(0)+":";
            File drive = new File(place);
            if (!drive.exists()) {
                yqZt.setErrorMsg("实物载体未连接到此电脑！");
                yqZt.setSavePath("未知");
                yqZt.setSurplus("-1");
                yqZt.setSurplusConvert("未知");
            }else {
                File file = new File(yqZt.getSavePath());
                Long freeSpaceInBytes = file.getFreeSpace();
                yqZt.setSurplus(freeSpaceInBytes+".0");
                yqZt.setSurplusConvert(YqZpgzServiceImpl.convertToReadableSize(freeSpaceInBytes+0.0));
            }
        }
        return yqZt;
    }

    /**
     * 查询载体列表
     * 
     * @param yqZt 载体
     * @return 载体
     */
    @Override
    public List<YqZt> selectYqZtList(YqZt yqZt)
    {
        return yqZtMapper.selectYqZtList(yqZt);
    }

    /**
     * 查询所有载体
     *
     * @param yqZt 载体
     * @return 载体
     */
    @Override
    public List<YqZt> selectYqZtListAll(YqZt yqZt)
    {
        return yqZtMapper.selectYqZtListAll(yqZt);
    }

    /**
     * 新增载体
     * 
     * @param yqZt 载体
     * @return 结果
     */
    @Override
    public String insertYqZt(YqZt yqZt)
    {
        int num=0;
        StringBuilder successMsg = new StringBuilder();
        if (yqZtMapper.isOnly(yqZt)!=0){
            successMsg.append("载体编号：”"+yqZt.getCode()+"”已存在！");
        }else {
            LoginUser user = SecurityUtils.getLoginUser();

            if(user == null){
                user = new LoginUser();
                user.setUserId(1L);
            }

            yqZt.setCreatorId(user.getUserId());
            yqZt.setCreateTime(DateUtils.getNowDate());
            if(yqZtMapper.insertYqZt(yqZt)>0){
                successMsg.append("添加成功！");
            };
        }
//        return num;
        return successMsg.toString();
    }

    /**
     * 修改载体
     * 
     * @param yqZt 载体
     * @return 结果
     */
    @Override
    public String updateYqZt(YqZt yqZt)
    {
        StringBuilder successMsg = new StringBuilder();
        if (yqZtMapper.selectYqZtByZtId(yqZt).getIsUse()==1){
            successMsg.append("载体编号为：“"+yqZt.getCode()+"”的载体已被使用，无法进行修改！");
        }else {
            if (yqZtMapper.isOnly(yqZt)!=0){
                successMsg.append("载体编号：”"+yqZt.getCode()+"”已存在！");
            }else {
                LoginUser user = SecurityUtils.getLoginUser();

                if(user == null){
                    user = new LoginUser();
                    user.setUserId(1L);
                }

                yqZt.setLastupdater(user.getUserId());
                yqZt.setLastupdateTime(DateUtils.getNowDate());
                if(yqZtMapper.updateYqZt(yqZt)>0){
                    successMsg.append("修改成功！");
                };
            }
        }
        return successMsg.toString();
    }

    /**
     * 批量删除载体
     * 
     * @param ztIds 需要删除的载体主键
     * @return 结果
     */
    @Override
    public String deleteYqZtByZtIds(Long[] ztIds)
    {
        StringBuilder failMsg = new StringBuilder();
        int successNum = 0;
        int failNum = 0;
        YqZt yqZt = new YqZt();
        for (int i = 0; i < ztIds.length; i++) {
            yqZt = selectYqZtByZtId(ztIds[i]);
            if (yqZt.getIsUse()==1){
                failMsg.append("编号为：“"+yqZt.getCode()+"”的载体已被使用，无法进行删除！\n");
                failNum++;
            }else {
                yqZtMapper.deleteYqZtByZtId(yqZt);
                successNum++;
            }
        }
        if (failNum==0){
            return "删除成功"+successNum+"条！";
        }else {
            return "删除成功"+successNum+"条，删除失败"+failNum+"条。\n失败原因："+failMsg;
        }
    }

    /**
     * 删除载体信息
     * 
     * @param ztId 载体主键
     * @return 结果
     */
    @Override
    public int deleteYqZtByZtId(Long ztId)
    {
        YqZt yqZt = new YqZt();
        yqZt.setZtId(ztId);
        return yqZtMapper.deleteYqZtByZtId(yqZt);
    }

    /**
     * 获取载体的下载记录
     *
     * @param ztId 载体主键
     * @return 结果
     */
    @Override
    public YqXzjl getDownloadRecord(Long ztId) {
        YqXzjl yqXzjl = new YqXzjl();
        yqXzjl.setZtId(ztId);
        return yqZpjlMapper.getDownloadRecord(yqXzjl);
    }
}
